<?php 
include_once 'conexion.php';

$REQUEST_ID_ = pg_escape_string(trim(isset($_REQUEST['ref_id']) ? $_REQUEST['ref_id'] : '0'));

// OBJETOS ESPECIFICOS DE DATOS
class Producto{ 
    private $id;
    public $data;
    function __construct($id) {
        $this->id = $id;
        $this->data = array("precio_venta"=>$this->getPrecioVenta(),"precio_costo"=>$this->getPrecioCosto(),"stock"=>$this->getStock());
    }
    
    function getPrecioVenta(){
        $strdata = '';
        $sqlstr = "SELECT prod_ultimo_precio+(prod_ultimo_precio*prod_ganancia) as prod_precio_venta, prod_presentacion_eg FROM producto ";
        $sqlstr .= " WHERE prod_id = {$this->id}";
        //echo $sqlstr;
        $consulta = getConexion()->retornar($sqlstr);
        while ($reg = pg_fetch_object($consulta)) {
            $strdata.= number_format($reg->prod_precio_venta, 2, '.', '').'###'.number_format($reg->prod_presentacion_eg, 2, '.', '');
        }
        return $strdata;
    }
    
    function getPrecioCosto(){
        $strdata = '';
        $sqlstr = "SELECT prod_ultimo_precio as prod_precio_costo, prod_presentacion_ing FROM producto ";
        $sqlstr .= " WHERE prod_id = {$this->id}";
        //echo $sqlstr;
        $consulta = getConexion()->retornar($sqlstr);
        while ($reg = pg_fetch_object($consulta)) {
            $strdata.= number_format($reg->prod_precio_costo, 2, '.', '').'###'.number_format($reg->prod_presentacion_ing, 2, '.', '');
        }
        return $strdata;
    }
    
    function getStock(){
        $cant = pg_escape_string(trim(isset($_REQUEST['cant']) ? $_REQUEST['cant'] : '0'));
        $depo = pg_escape_string(trim(isset($_REQUEST['depo']) ? $_REQUEST['depo'] : '0'));
        $strdata = '';
        $sqlstr = "SELECT (prod_comprado-prod_vendido+prod_stock_manual) as prod_stock, prod_minimo ".
            "FROM vw_producto_stock ".
            "WHERE (prod_id = {$this->id}) ";
        //echo $sqlstr;
        $consulta = getConexion()->retornar($sqlstr);
        while ($reg = pg_fetch_object($consulta)) {
            $strdata.= number_format(($reg->prod_stock-$cant), 2, '.', '').'###'.number_format($reg->prod_minimo, 2, '.', '');
        }
        $bydepo='0';
        if($depo != '0'){
            $bydepo = $this->getStockByDeposito($cant, $depo);
        }
        return $strdata.'###'.$bydepo;
    }
    
    function getStockByDeposito($cant, $depo){        
        $strdata = '';
        $sqlstr = "SELECT depo_id, prod_id, (vw.prod_comprado - vw.prod_vendido + vw.prod_stock_manual) prod_stock ".
                "FROM vw_depo_prod_stock vw WHERE vw.prod_id = {$this->id} AND vw.depo_id = {$depo}";
        //echo $sqlstr;
        $consulta = getConexion()->retornar($sqlstr);
        while ($reg = pg_fetch_object($consulta)) {
            $strdata.= number_format(($reg->prod_stock-$cant), 2, '.', '');
        }
        return $strdata;
    }
}

class EgresoDet{
    function getStrData($data){
        $strdata = '';
        $end = '';
        $_id = pg_escape_string(trim($_id));
        $sqlstr = "SELECT egdet_id, egdet_prod_id FROM egreso_det ";
        $sqlstr .= " WHERE egdet_id = {$_id}";
        $sqlstr .= " ORDER BY egdet_id";
        //echo $sqlstr;
        $consulta = getConexion()->retornar($sqlstr);
        while ($reg = pg_fetch_object($consulta)) {
            $strdata.= $end . $reg->egdet_id;
            $end = chr(29);
        }
        return $strdata;
    }
}
?>
<?php 
$myCollection = new GenericArray();
// AGREGAR OBJETOS ESPECIFICOS AL ARRAY DE OBJETOS GENERICOS
$myCollection->addObject('producto', new Producto($REQUEST_ID_));
//$myCollection->addObject('egreso_det', new EgresoDet());

// MOSTRAR EL RESULTADO DEL OBJETO QUE SE ENCUENTRA EN EL INDICE REQUERIDO
echo $myCollection->getObject($_REQUEST['obj'])->data[$_REQUEST['data']]; 

?> 
